<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.xq.shop.po.Menu"%>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<jsp:include page="/jsp/admin/common.jsp" />
<jsp:include page="/jsp/tableHead.jsp" />
<script type="text/javascript">
$().ready(function(){

	var option = {
			head : [
		            {name:"name",title:"名称",width:300},
		            {name:"seq",title:"排序",width:100},
		            {name:"operation",title:"操作",width:300}
		            ],//表格标题定义 
            table_Option : {
				head : {sortable: false, selectAll: {require:false}},
				row : { selectable : false },
				foot : { require : false, pageNum : 1, pageSize : 100}
			},
            addURL : "<%=basePath %>admin/category_edit.do",
            searchURL : "<%=basePath %>admin/category_list.do",
            editURL : "<%=basePath %>admin/category_edit.do",
            deleteURL : "<%=basePath %>admin/category_delete.do",
            successFn : function(_result){

            	/*list存放一级分类，tmpList存放二级分类的父级分类，如果遍历的结果存在父级分类，在tmpList中删除内容*/
				var row, list = [], row_pk, tmpList = {};
				for(var i=0, len=_result.page.length; i<len; i++){
					row = _result.page[i];

					if(row["grade"] == 1){

						//list[row["seq"]] = row;	//一维数组存放一级分类

						row_pk = "pk_" + row["id"];
						var obj = {};
						obj.id = row_pk;
						obj.seq = row["seq"];

						if(tmpList[row_pk]){

							obj.position = tmpList[row_pk]["position"];
						} else {
							obj.position = list.length;
						}

						tmpList[row_pk] = obj;
						list[obj.position] = row;
						
						if(!list[row_pk]) list[row_pk] = [];	//数组属性存放二级分类

						row["name"] = "<span style='margin-left:0px; color:#000000;'>" + row["name"] + "</span>";
					} else if(row["grade"] == 2){

						var row_pk = "pk_" + row["parent_id"];

						if(!tmpList[row_pk]){

							var obj = {};
							obj.id = row_pk;
							obj.seq = list.length;
							obj.position = list.length;	//数据在list中的存放位置
							list[list.length] = {id : row["parent_id"]};
							tmpList[row_pk] = obj;
						}
						
						if(!list[row_pk]){	//上级分类未被遍历，先添加下级分类
							list[row_pk] = [];
						}
						list[row_pk].push(row);
						
						row["name"] = "<span style='margin-left:20px;'>" + row["name"] + "</span>";
					}
					var $a = $("<a/>").attr("href", basePath + "admin/category_edit.do?id=" + row["id"] + "&new=" + Math.random()).html("[编辑]");
					var $a2 = $("<a/>").attr("href", "javascript:default_Option.rowDeleteFn(" + row["id"] + ")").html("[删除]");
					row["operation"] = $a.prop("outerHTML")+ "&nbsp;" + $a2.prop("outerHTML");
				}

				_result.page = [];

				var group = [];
				for(var i in tmpList){
					group.push(tmpList[i]);
				}
				group.sort(function(row1, row2){
					if(row1["seq"] > row2["seq"]) return 1;
					else return -1;
				});
				
				for(var i=0, len=group.length; i<len; i+=1){

					_result.page.push(list[group[i]["position"]]);
					row_pk = "pk_" + list[group[i]["position"]]["id"];
					list[row_pk].sort(function(row1, row2){
						if(row1["seq"] > row2["seq"]) return 1;
						else return -1;
					})
					_result.page = _result.page.concat(list[row_pk]);
				}

				default_Option.$table.refresh(_result);
				$("#pageTotal").html(_result.total);
            }
	};
	initTable(option);
});
</script>
</head>
<body onload="">
<div class="path">
	<a href="">首页</a> &raquo; 商品分类列表<span>(共<span id="pageTotal"></span>条记录)</span>
</div>
<div class="bar">
	<jsp:include page="../search.jsp">
		<jsp:param value="name,名称,LIKE,true" name="searchKey"/>
		<jsp:param value="true" name="delete"/>
		<jsp:param value="true" name="page"/>
	</jsp:include>
</div>
<table id="listTable" class="list">
</table>
</body>
</html>